package org.openkoala.koala.wizard.wizardpage;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.IWorkbench;
import org.openkoala.koala.widget.Module;
import org.openkoala.koala.widget.Project;
import org.openkoala.koala.wizard.wizardpage.component.ModuleManage;

/**
 * 项目模块信息管理界面
 * @author xmfang
 *
 */
public class ModulesInfoWizardPage extends WizardPage {

	private ModuleManage moduleManage;
	
	private Project project;
	
	public ModulesInfoWizardPage(IWorkbench workbench, Project project) {
		super("module information");
		this.project = project;
		setTitle("Add Modules");
		setDescription("Add modules to the project.");
		setPageComplete(false);
	}
	
	@Override
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setFont(parent.getFont());
		composite.setLayout(new GridLayout(1, false));
		
//		project.setModule(new ArrayList<Module>());
		
		moduleManage = new ModuleManage(project);
		moduleManage.getControl(composite);
		
		Dialog.applyDialogFont(composite);
//		PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IJavaHelpContextIds.NEW_JAVAPROJECT_WIZARD_PAGE);
		editRepositoryImpl(composite);
		setPageComplete(true);
		setControl(composite);
	}
	
	private Composite editRepositoryImpl(Composite parent) {
		Composite result = new Composite(parent, SWT.NONE);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		result.setLayout(gridLayout);
		
		Label label = new Label(result, SWT.NONE);

		label.setText("Repository Implement");
		final Combo combo = new Combo(result, SWT.SINGLE | SWT.READ_ONLY);
		combo.add("JPA");
		combo.add("Mybatis");
		combo.select(0);
		combo.addSelectionListener(new SelectionAdapter(){
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				project.setDbProtocol(combo.getText());
			}});
		
		Label mvcLabel = new Label(result, SWT.NONE);
		mvcLabel.setText("MVC Implement");
		
		final Combo mvcCombo = new Combo(result, SWT.SINGLE | SWT.READ_ONLY);
		mvcCombo.add("SpringMVC");
		mvcCombo.add("Struts2MVC");
		mvcCombo.select(0);
		mvcCombo.addSelectionListener(new SelectionAdapter(){
			@Override
			public void widgetSelected(SelectionEvent event) {
				project.setMvcProtocol(mvcCombo.getText());
			}});
		return result;
	}
	
	@Override
	 public IWizardPage getNextPage() {
		if (haveNoWarModule()) {
			InfoSummaryWizardPage infoSummaryPage = (InfoSummaryWizardPage) getWizard().getPage("Project Information");
			if (infoSummaryPage.getControl() != null) {
				infoSummaryPage.refreshProjectInfo();
			}
			return infoSummaryPage;
		}
		
		ModuleConfigWizardPage securityWizardPage = (ModuleConfigWizardPage) getWizard().getPage("SubSystem Config");
		securityWizardPage.refreshWebModules();
		return super.getNextPage();
	}
	
	private boolean haveNoWarModule() {
		for (Module module : project.getModule()) {
			if (module.getModuleType().equals("war")) {
				return false;
			}
		}
		return true;
	}
	
}
